package org.csu.mybigpro.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.mybigpro.domain.AttendanceRecord;
import org.csu.mybigpro.persistence.AttendanceRecordMapper;
import org.csu.mybigpro.service.IAttendanceService;
import org.csu.mybigpro.util.UserContext;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceRecordMapper, AttendanceRecord> implements IAttendanceService {

    @Override
    public AttendanceRecord clockIn(String clockInContext) {
        Integer currentUserId = UserContext.getCurrentUserId();
        if (currentUserId == null) {
            throw new IllegalStateException("无法获取当前用户信息，打卡失败");
        }

        AttendanceRecord record = new AttendanceRecord();
        record.setUserId(currentUserId);
        record.setRoomName(clockInContext); // 使用通用字段记录打卡场景
        record.setClockInTime(LocalDateTime.now());
        record.setStatus("ON_TIME"); // 简化处理，可根据规则判断是否迟到

        this.save(record);
        return record;
    }
}
